$(function (){
    if(error){
    layer.alert(error);
    }

    $("#kindCombo").click(function () {
        if($("#kindCombo>option:first-child").val() == "none") {
            fetchKind();
            fetchFood();

        }
    });

    $("#kindCombo").change(function () {
        fetchFood();
    })

    if(food_id.trim() !== ""){
        $("#foodCombo").children("[name="+food_id+"]").prop("selected",true);
    }

    $("#sub-btn").click(function (){
        let emp_id =$(":input[name = emp_id]").val();
        if(emp_id.trim() === ""){
            layer.msg("顾客编号不可为空");
            return false;
        }
        let ord_id = $(":input[name= ord_id]").val();
        if(ord_id.trim() === ""){
            layer.msg("订单编号不可为空");
            return  false;
        }
        let food_id = $(":input[name= food_id]").val();
        if(food_id.trim() === ""){
            layer.msg("食品编号不可为空");
            return  false;
        }
        let text = $(":input[name = food_id]").val();
        let $option = $("#foodCombo").children("[name=" + text + "]")
        if ($option.length == 0) {
            layer.msg("该分类编号不存在");
            return;
        }
        console.log("前端校验不错")
        $("#emp-form").submit();
    });
        $("#foodCombo").change(function () {
            $(":input[name = food_id]").val($(this).val());
        });
        $(":input[name = food_id]").blur(function () {
            let text = $(":input[name = food_id]").val();
            let $option = $("#foodCombo").children("[name=" + text + "]")
            if ($option.length == 0) {
                layer.msg("该分类编号不存在");
                return;
            }
            $option.prop("selected", true);
        });

});
function fetchKind(){
    layer.msg("触发");

    let url = ctx + "/kind/comboList";
    $.post(url,{},function(resp){
        let klist = resp.klist;
        $("#kindCombo").empty();
        let index=0;
        klist.forEach(function (item){
            if(index ===0){
                $("#kindCombo").append($("<option value='"+item.kindId+"' name='"+item.kindId+"' selected='selected'>"+item.name+"</option>"));

                index++;
            }
            else{
                $("#kindCombo").append($("<option value='"+item.kindId+"' name='"+item.kindId+"'>"+item.name+"</option>"));
            }
        });
    });

}
function fetchFood(){
    // layer.msg("触发");

    let url = ctx + "/food/comboList";
    $.post(url,{},function(resp){
        let flist = resp.flist;
        $("#foodCombo").empty();
        let k = $("#kindCombo>option:selected");
        console.log(k.get(0));
        let index=0;
        flist.forEach(function (item){
            if(k.val() == item.kindId){
                if(index === 0){
                    $(":input[name = food_id]").val(item.foodId);
                    index++;
                }
                $("#foodCombo").append($("<option value='"+item.foodId+"' name='"+item.foodId+"'>"+item.name+"</option>"));
            }
        });
    });

}